#include <stdio.h>
#include <string.h>

int main( )
{
    int t, a, b, c, k, mint, maxt;
    char minr[ 50 ], maxr[ 50 ],  tmp, name[ 50 ];
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d", &k);
        mint = 86401;
        maxt = -1;
        while ( k-- )
        {
            scanf("%s", name);
            scanf("%d%c%d%c%d", &a, &tmp, &b, &tmp, &c);
            a = a * 3600 + b * 60 + c;
            if ( mint > a )
            {
                mint = a;
                strcpy( minr, name );
            }
            scanf("%d%c%d%c%d", &a, &tmp, &b, &tmp, &c);
            a = a * 3600 + b * 60 + c;
            if ( maxt < a )
            {
                maxt = a;
                strcpy( maxr, name );
            }
        }
        printf("%s %s\n", minr, maxr);
    }
    return 0;
}
